﻿using mozhi.smarterp.AuthorCenter.Permissions.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;

namespace mozhi.smarterp.AuthorCenter.Permissions
{
    public class PermissionGroupDataSeeder : IPermissionGroupDataSeeder, ITransientDependency
    {
        private IPermissionGroupRepository _repository;
        private IGuidGenerator _guidGenerator;

        public PermissionGroupDataSeeder(IPermissionGroupRepository repository, IGuidGenerator guidGenerator)
        {
            _repository = repository;
            _guidGenerator = guidGenerator;
        }

        public async Task SeedAsync()
        {
            var count = await _repository.GetCountAsync();

            if (count > 0)
                return;

            var identityPerrmissionGroup = new PermissionGroup(_guidGenerator.Create(), "AbpIdentity", "认证中心", Enums.PermissionType.PlatformItem);

            var rolePermissionId = _guidGenerator.Create();

            var rolePermissionItems = new List<PermissionItem>()
            {
                new PermissionItem(rolePermissionId , null , "AbpIdentity.Roles" , "角色" , Enums.PermissionType.PlatformItem),

                new PermissionItem(_guidGenerator.Create() , rolePermissionId , "AbpIdentity.Roles.Create" , "新增" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , rolePermissionId , "AbpIdentity.Roles.Delete" , "删除" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , rolePermissionId , "AbpIdentity.Roles.ManagePermissions" , "查看" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , rolePermissionId , "AbpIdentity.Roles.Update" , "修改" , Enums.PermissionType.PlatformItem),
            };


            identityPerrmissionGroup.AddItems(rolePermissionItems);

            var userPermissionId = _guidGenerator.Create();

            var userPermissionItems = new List<PermissionItem>()
            {
                new PermissionItem(userPermissionId , null , "AbpIdentity.Users" , "用户" , Enums.PermissionType.PlatformItem),

                new PermissionItem(_guidGenerator.Create() , userPermissionId , "AbpIdentity.Users.Create" , "新增" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , userPermissionId , "AbpIdentity.Users.Delete" , "删除" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , userPermissionId , "AbpIdentity.Users.ManagePermissions" , "查看" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , userPermissionId , "AbpIdentity.Users.Update" , "修改" , Enums.PermissionType.PlatformItem),
            };

            identityPerrmissionGroup.AddItems(userPermissionItems);

            var tenantPermissionId = _guidGenerator.Create();

            var tenantPermissionItems = new List<PermissionItem>()
            {
                new PermissionItem(tenantPermissionId , null , "AbpTenantManagement.Tenants" , "租户" , Enums.PermissionType.PlatformItem),

                new PermissionItem(_guidGenerator.Create() , tenantPermissionId , "AbpTenantManagement.Tenants.Create" , "新增" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , tenantPermissionId , "AbpTenantManagement.Tenants.Delete" , "删除" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , tenantPermissionId , "AbpTenantManagement.Tenants.ManageConnectionStrings" , "数据库管理" , Enums.PermissionType.PlatformItem),
                new PermissionItem(_guidGenerator.Create() , tenantPermissionId , "AbpTenantManagement.Tenants.Update" , "修改" , Enums.PermissionType.PlatformItem),
            };

            identityPerrmissionGroup.AddItems(tenantPermissionItems);

            await _repository.InsertAsync(identityPerrmissionGroup , true);

        }
    }
}
